<%
'' -------------------------------------------------------------------
''
''  Microsoft Commerce Server 2000
''
''  Copyright 1996 - 2000 Microsoft Corporation. All Rights Reserved.
''
''  File: DlgDateSel.htm
''
''  Description: Displays a date selection dialog and
''  allows user to choose a date.
''  Returns Null if Cancel is clicked.
''  Returns a date in the localized format if OK is clicked.
''  Used in the query builder.
''  Input Dialog Arguments:
''    ...HelpTopic (optional) -- URL for a help topic to display
''    ...DateFormat (optional) -- A localized date format (m/d/y is default)
''    ...FirstDay (optional) -- Specifies the first day of the week in 
''                              the calendar display (0=Sunday is default)
''
'' -------------------------------------------------------------------
%>

<HTML>
<HEAD>
<TITLE ID='L_DateSel_DialogTitle'>Date Selector</TITLE>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=windows-1252'>
<LINK REL='stylesheet' TYPE='text/css' HREF='/widgets/bizdesk.css'>
<LINK REL='stylesheet' TYPE='text/css' HREF='/widgets/exprbldrhtc/exprbldr.css'>

<SCRIPT LANGUAGE='VBScript'>
Option Explicit

Const DEFAULT_DATEFORMAT = "m/d/y"  ' Default for the date format
Const DEFAULT_FIRSTDAY = 0          ' Default first day is Sunday
Const DEFAULT_SEPARATOR = "/"       ' Default date separator

Dim m_strHelpTopic         ' Help Topic URL
Dim winHelpWindow          ' Window name used to display help
Dim m_rgDateFormat(2)      ' Array for localized date format parts
Dim m_strSeparator         ' Separator for date format (/, -, or .)

window.returnValue = Null

' --------------------------------------------------------
' Function:
'     initDialog()
'
' Description:
'     Gets the dialog arguments and initializes the following:
'      -Help button
'      -Date format & first day
'      -Default date
' ---------------------------------------------------------
Sub initDialog()

    Dim btnhelp
    Dim strDateFormat
    Dim nFirstDay
    Dim strDefault
    Dim objArgs
    Set objArgs = window.dialogArguments

    ' Only display help button if there is a topic
    m_strHelpTopic = objArgs.Item("HelpTopic")
    If IsNull(m_strHelpTopic) Then
        ' Hide the help button
        Set btnhelp = L_Help_Button
        btnHelp.style.display = "none"
    End If

    ' The format for dates should be passed in
    strDateFormat = objArgs.Item("DateFormat")
    If IsNull(strDateFormat) Then
        FormatDate DEFAULT_DATEFORMAT
    Else
        FormatDate strDateFormat
    End If

    ' Get first day config
    nFirstDay = objArgs.Item("FirstDay")
    If IsNull(nFirstDay) Then
        divDatePicker.firstday = DEFAULT_FIRSTDAY
    Else
        divDatePicker.firstday = nFirstDay
    End If

    ' Get date to start with, if available
    strDefault = objArgs.Item("Default")
    If Not IsNull(strDefault) Then
        SetDate strDefault
    End If

End Sub

' --------------------------------------------------------
' Function:
'     FormatDate(strDateFormat)
'
' Description:
'     Using a passed-in date format, populates an array to
'     describe the order of the month, day and year.
'     Also writes the separator (/, -, or .) to the global
'     variable m_strSeparator.
' ---------------------------------------------------------
Sub FormatDate(strDateFormat)

    Dim rgDate
    Dim sItem
    Dim i

    ' Separator may only be "/", "-", or "."
    If inStr(strDateFormat, "/") Then
        m_strSeparator = "/"
    ElseIf inStr(strDateFormat, "-") Then
        m_strSeparator = "-"
    ElseIf inStr(strDateFormat, ".") Then
        m_strSeparator = "."
    Else
        ' Invalid separator - revert To default
        strDateFormat = DEFAULT_DATEFORMAT
        m_strSeparator = DEFAULT_SEPARATOR
    End If

    ' Now format the date properly
    rgDate = Split(strDateFormat, m_strSeparator)
    If UBound(rgDate) = 2 Then
        For i = 0 To UBound(rgDate)
            sItem = LCase(left(rgDate(i), 1))
            If sItem = "d" Or sitem = "m" Or _
               sItem = "y" Then
                m_rgDateFormat(i) = sItem
            Else
                ' Something is wrong with the format
                ' ...so use the default
                FormatDate DEFAULT_DATEFORMAT
                Exit For
            End If
        Next
    Else
        ' Something is wrong with the format
        ' ...so use the default
        FormatDate DEFAULT_DATEFORMAT
    End If

End Sub

' --------------------------------------------------------
' Function:
'     SetDate(strDate)
'
' Description:
'     Parses the specified date using the localized format
'     and sets the date on the calendar control.
'     Only sets the date if it valid according to the
'     specified localized format.
' ---------------------------------------------------------
Sub SetDate(strDate)

    Dim rgDate
    Dim nDay, nMonth, nYear
    Dim i

    rgDate = Split(strDate, m_strSeparator)
    If UBound(rgDate) = 2 Then
        ' This is a valid date
        For i = 0 To UBound(m_rgDateFormat)
            Select Case m_rgDateFormat(i)
                ' Day
                Case "d"
                    nDay = rgDate(i)
                    ' Month
                Case "m"
                    nMonth = rgDate(i)
                    ' Year
                Case "y"
                    nYear = rgDate(i)
            End Select
        Next
        divDatePicker.setDate nDay, nMonth, nYear
    End If

End Sub

' --------------------------------------------------------
' Function:
'    onBtnOK()
'
' Description:
'     Called when user clicks the OK button .
'     Gets the day, month, and year from the calendar
'     and builds a date based on the localized format.
'     Sets the return value to the new date and
'     closes the dialog window.
' ---------------------------------------------------------
Sub onBtnOK()

    Dim rgDate(2)
    Dim i

    ' Now format the date properly
    For i = 0 To UBound(m_rgDateFormat)
        Select Case m_rgDateFormat(i)
            ' Day
            Case "d"
                rgDate(i) = divDatePicker.cday
                ' Month
            Case "m"
                rgDate(i) = divDatePicker.cmonth
                ' Year
            Case "y"
                rgDate(i) = divDatePicker.cyear
        End Select
    Next

    ' Pass back the return-value.
    window.returnValue = join(rgDate, m_strSeparator)
    window.close

End Sub

' --------------------------------------------------------
' Function:
'     onBtnCancel()
'
' Description:
'     Called when user clicks the Cancel button.
'     Leaves the return value as Null and
'     closes the dialog window.
' ---------------------------------------------------------
Sub onBtnCancel()

    window.close

End Sub

' --------------------------------------------------------
' Function:
'     onBtnHelp()
'
' Description:
'     Called when user clicks the Help button.
'     If a help window is not already open, opens a new
'     help window in IE.
'     Navigates to the specified help page.
' ---------------------------------------------------------
Sub onBtnHelp()

    If Not IsNull(m_strHelpTopic) Then
        If typename(winHelpWindow) = "HTMLWindow2" Then 
            winHelpWindow.close
        End If
        Set winHelpWindow = window.open(m_strHelpTopic, "winHelpWindow", _
         "height=500,width=700,status=no,toolbar=yes,menubar:no,resizable=yes")
    End If

End Sub

</SCRIPT>

</HEAD>
<BODY ONLOAD='initDialog' STYLE="margin:0px;padding:10px">

<ID ID='L_DateSel_StaticText'>Choose a date:</ID>
<DIV ID='divDatePicker' ALIGN='center'
    STYLE='margin-top:4px; behavior:url(/widgets/editHTC/calendar.htc)'>
</DIV>

<DIV ALIGN='right' STYLE='padding-top:10px'>
    <BUTTON ID='L_OK_Button' STYLE='width:60px' onclick='onBtnOK'>OK</BUTTON>
    <SPAN STYLE='width:2px; margin:0px; padding:0px'></SPAN>
    <BUTTON ID='L_Cancel_Button' STYLE='width:60px'onclick='onBtnCancel'>Cancel</BUTTON>
    <SPAN STYLE='width:2px; margin:0px; padding:0px'></SPAN>
    <BUTTON ID='L_Help_Button' STYLE='width:60px'onclick='onBtnHelp'>Help</BUTTON>
</DIV>

</BODY>
</HTML>
